﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using Skyhook.Core.Graphics.Options;

namespace Skyhook.Core.Graphics
{
    public static class GraphicsManager
    {
        static GraphicsDevice GraphicsDevice = null;
        static GraphicsDeviceManager GraphicsDeviceManager = null;

        internal static void Initialize(Game game, GraphicsData graphicsdata)
        {
            GraphicsDeviceManager = new GraphicsDeviceManager(game);
            GraphicsDeviceManager.PreferredBackBufferHeight = 768;
            GraphicsDeviceManager.PreferredBackBufferWidth = 1024;
            GraphicsDeviceManager.IsFullScreen = false;
            GraphicsDeviceManager.PreferredDepthStencilFormat = DepthFormat.Depth16;
            GraphicsDeviceManager.SynchronizeWithVerticalRetrace = false;

            GraphicsDeviceManager.GraphicsProfile = GraphicsProfile.HiDef;

            GraphicsDeviceManager.PreparingDeviceSettings += new EventHandler<PreparingDeviceSettingsEventArgs>(
                delegate(object sender, PreparingDeviceSettingsEventArgs e)
                {
                    e.GraphicsDeviceInformation.PresentationParameters.RenderTargetUsage = RenderTargetUsage.PreserveContents;
                }
            ); 
        }

        internal static void SetGraphicsDevice(GraphicsDevice graphicsdevice)
        {
            GraphicsDevice = graphicsdevice;
            //GraphicsDevice.PresentationParameters.RenderTargetUsage = RenderTargetUsage.PreserveContents;
        }

        public static void ClearScreen(Color color)
        {
            GraphicsDevice.Clear(color);
        }

        public static void SetVertexBuffer(VertexBuffer buffer)
        {
            GraphicsDevice.SetVertexBuffer(buffer);
        }

        public static void SetIndexBuffer(IndexBuffer buffer)
        {
            GraphicsDevice.Indices = buffer;
        }

        public static void DrawIndexedPrimitives(PrimitiveType primitivetype, int basevertex, int minvertexindex, int numverticles, int startindex, int primitivescount)
        {
            GraphicsDevice.DrawIndexedPrimitives(primitivetype, basevertex, minvertexindex, numverticles, startindex, primitivescount);
        }

        public static int ViewportWidth
        {
            get
            {
                return GraphicsDevice.Viewport.Width;
            }
        }

        public static int ViewportHeight
        {
            get
            {
                return GraphicsDevice.Viewport.Height;
            }
        }

        public static void SetRenderTarget(RenderTarget2D rendertarget)
        {
            GraphicsDevice.SetRenderTarget(rendertarget);
        }
    }
}
